import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { Session } from '/@/utils/storage';
import { useRouter } from 'vue-router';
import defaultUser from '/@/assets/images/icon-user.png';
import { fetchUserInfo } from '/@/api/api';
import { fetchHasNotice } from '/@/api/system';
import { ElNotification } from "element-plus";
 
const router = useRouter();
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosState => ({
		userInfos: {
			userName: '',
			mobile: '',
			time: 0,
			permissionCodes: [],
		},
		hasUnreadMessage: false,
	}),
	actions: {
		async setUserInfos() {
			// 存储用户信息到浏览器缓存
				const userInfo:any = await fetchUserInfo();
				// 2.判断当前用户有没有菜单权限
				if (!userInfo.data.permissionCodes.length) {
					ElNotification({
						title: "无权限访问",
						message: "当前账号无任何菜单权限，请联系系统管理员！",
						type: "warning",
						duration: 3000
					});
					Session.set('token','');
					router.push('/login');
					return new Error("No permission");
				}
				this.userInfos = userInfo.data;
			
		},
		async getUnreadMessage() {
			const res:any = await fetchHasNotice({});
			this.hasUnreadMessage = res.data;
			console.log(this.hasUnreadMessage);
		},
	},
});
